package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺的控制层
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理")
public class ShopController {

    /**
     * 注入service对象
     */
    @Autowired
    private IShopService shopService;

    /**
     * 激活店铺
     * @return 激活店铺
     */
    @GetMapping("/active/{uuid}/{shopId}")
    @ApiOperation(value = "激活店铺",notes = "激活店铺")
    public AjaxResult activeShop(@PathVariable("uuid") String uuid,@PathVariable("shopId") String shopId){

        return AjaxResult.createSuccess(shopService.activeShop(uuid,shopId));
    }

    /**
     * 查询所有
     * @return 店铺列表
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有店铺",notes = "可以查询所有的店铺")
    public AjaxResult findAll(){
        return AjaxResult.createSuccess(shopService.findAll());
    }
    /**
     * 店铺入驻
     * @return 店铺入驻
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻",notes = "店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){

        //店铺入驻
        shopService.settlement(shop);

        return AjaxResult.createSuccess();
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询",notes = "可以分页查询所有的店铺")
    public AjaxResult findPage(@RequestBody ShopQuery query){
        return AjaxResult.createSuccess(shopService.findPage(query));
    }

    /**
     * 新增或者修改,根据id区分
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改",notes = "可以根据id区分操作")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){

        //根据id区分,没有id就是新增
        try {
            shopService.saveOrUpdate(shop);

            return AjaxResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常,联系管理员李裕");
        }
    }

    /**
     * 删除店铺根据id
     * @return
     * 接受请求： /emp/12
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作",notes = "根据id删除店铺")
    public AjaxResult delete(@PathVariable("id") Long id){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            shopService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常,联系管理员李裕");
        }
    }

}